import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/match_detail/live/live_panel.dart';
import 'package:ball_lion/module/overlay/live_float_widget.dart';
import 'package:ball_lion/module/live/ali_live_logic.dart';
import 'package:ball_lion/entity/live/live_detail_entity.dart';
import 'package:ball_lion/entity/match/match_live_entity.dart';
import 'package:ball_lion/widgets/player/qs_player_logic.dart';
import 'package:ball_lion/widgets/player/qs_player.dart';

class MatchLiveView extends StatefulWidget {
  final MatchLiveEntity? liveInfo;
  final int sportsId;
  const MatchLiveView({super.key, this.liveInfo, required this.sportsId});

  @override
  State<MatchLiveView> createState() => _MatchLiveViewState();
}

class _MatchLiveViewState extends State<MatchLiveView> {
  late final AliLiveLogic logic;
  late final QsPlayerLogic playerLogic;

  @override
  void initState() {
    try {
      logic = Get.find<AliLiveLogic>(tag: "${widget.liveInfo?.matchId}");
    } catch (e) {
      logic = Get.put(AliLiveLogic(), tag: "${widget.liveInfo?.matchId}");
    }
    logic.playerLogic =
        Get.find<QsPlayerLogic>(tag: "${widget.liveInfo?.matchId}");
    try {
      playerLogic = Get.find<QsPlayerLogic>(tag: "${widget.liveInfo?.matchId}");
      playerLogic.url = widget.liveInfo?.url ?? "";
      LiveFloatController.instance.prepare(
        LiveDetailEntity.fromMatch(widget.liveInfo, widget.sportsId),
      );
    } catch (e) {
      playerLogic =
          Get.put(QsPlayerLogic(), tag: "${widget.liveInfo?.matchId}");
      playerLogic.url = widget.liveInfo?.url ?? "";
      LiveFloatController.instance.prepare(
        LiveDetailEntity.fromMatch(widget.liveInfo, widget.sportsId),
      );
    }
    if (logic.roomInfo != null) {
      if (logic.roomInfo?.expertId != widget.liveInfo?.expertId) {
        logic.roomInfo = widget.liveInfo;
        logic.doRefresh();
      } else {
        playerLogic.doRefresh();
      }
    } else {
      logic.roomInfo = widget.liveInfo;
    }
    logic.sportsId = widget.sportsId;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AliLiveLogic>(
        tag: "${widget.liveInfo?.matchId}",
        builder: (logic) {
          return PopScope(
            canPop: false,
            onPopInvoked: (didPop) {
              if (didPop) return;
              logic.back();
            },
            child: Scaffold(
              backgroundColor: Colors.white,
              body: OrientationBuilder(
                  builder: (context, orientation) => Hero(
                        tag: "player",
                        child: SizedBox(
                          width: Get.width,
                          height: Get.height,
                          child: Stack(
                            children: [
                              Positioned.fill(
                                child: QsPlayer(
                                  logic: playerLogic,
                                ),
                              ),
                              LivePanel(logic: logic)
                            ],
                          ),
                        ),
                      )),
            ),
          );
        });
  }
}
